<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>Freetype OpenGL: demo-texture.c</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="style.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="content"><!-- do not remove this div! -->

<div id="top"><!-- do not remove this div! -->

  
  <div id="titlearea">
    
    <div id="projectname">Freetype OpenGL</div>
    
    
    <div id="projectbrief">A high quality OpenGL text rendering engine for C</div>
    
  </div>
  

<!--

<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">Freetype OpenGL
   &#160;<span id="projectnumber">0.1</span>
   </div>
   <div id="projectbrief">A high quality OpenGL text rendering engine for C</div>
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>
-->


<!-- Generated by Doxygen 1.8.0 -->
<script type="text/javascript" src="dynsections.js"></script>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">demo-texture.c</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><pre class="fragment"><span class="comment">/* ============================================================================</span>
<span class="comment"> * Freetype GL - A C OpenGL Freetype engine</span>
<span class="comment"> * Platform:    Any</span>
<span class="comment"> * WWW:         http://code.google.com/p/freetype-gl/</span>
<span class="comment"> * ----------------------------------------------------------------------------</span>
<span class="comment"> * Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.</span>
<span class="comment"> *</span>
<span class="comment"> * Redistribution and use in source and binary forms, with or without</span>
<span class="comment"> * modification, are permitted provided that the following conditions are met:</span>
<span class="comment"> *</span>
<span class="comment"> *  1. Redistributions of source code must retain the above copyright notice,</span>
<span class="comment"> *     this list of conditions and the following disclaimer.</span>
<span class="comment"> *</span>
<span class="comment"> *  2. Redistributions in binary form must reproduce the above copyright</span>
<span class="comment"> *     notice, this list of conditions and the following disclaimer in the</span>
<span class="comment"> *     documentation and/or other materials provided with the distribution.</span>
<span class="comment"> *</span>
<span class="comment"> * THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER &#39;&#39;AS IS&#39;&#39; AND ANY EXPRESS OR</span>
<span class="comment"> * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF</span>
<span class="comment"> * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO</span>
<span class="comment"> * EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,</span>
<span class="comment"> * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES</span>
<span class="comment"> * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;</span>
<span class="comment"> * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND</span>
<span class="comment"> * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<span class="comment"> * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF</span>
<span class="comment"> * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<span class="comment"> *</span>
<span class="comment"> * The views and conclusions contained in the software and documentation are</span>
<span class="comment"> * those of the authors and should not be interpreted as representing official</span>
<span class="comment"> * policies, either expressed or implied, of Nicolas P. Rougier.</span>
<span class="comment"> * ============================================================================</span>
<span class="comment"> *</span>
<span class="comment"> * Example showing texture atlas packing</span>
<span class="comment"> *</span>
<span class="comment"> * ============================================================================</span>
<span class="comment"> */</span>
<span class="preprocessor">#include &quot;freetype-gl.h&quot;</span>



<span class="comment">// ---------------------------------------------------------------- display ---</span>
<span class="keywordtype">void</span> display( <span class="keywordtype">void</span> )
{
    <span class="keywordtype">int</span> viewport[4];
    glGetIntegerv( GL_VIEWPORT, viewport );
    GLuint width  = viewport[2];
    GLuint height = viewport[3];

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glBegin(GL_QUADS);
    glTexCoord2f( 0, 1 ); glVertex2i( 0, 0 );
    glTexCoord2f( 0, 0 ); glVertex2i( 0, height );
    glTexCoord2f( 1, 0 ); glVertex2i( width, height );
    glTexCoord2f( 1, 1 ); glVertex2i( width, 0 );
    glEnd();
    glutSwapBuffers( );
}


<span class="comment">// ---------------------------------------------------------------- reshape ---</span>
<span class="keywordtype">void</span> reshape( <span class="keywordtype">int</span> width, <span class="keywordtype">int</span> height )
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, width, 0, height, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glutPostRedisplay();
}


<span class="comment">// --------------------------------------------------------------- keyboard ---</span>
<span class="keywordtype">void</span> keyboard( <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> key, <span class="keywordtype">int</span> x, <span class="keywordtype">int</span> y )
{
    <span class="keywordflow">if</span> ( key == 27 )
    {
        exit( 1 );
    }
}

<span class="comment">// ------------------------------------------------------------------- main ---</span>
<span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv )
{
    glutInit( &amp;argc, argv );
    glutInitWindowSize( 512, 512 );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
    glutCreateWindow( argv[0] );
    glutReshapeFunc( reshape );
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );

    <a name="_a0"></a><a class="code" href="structtexture__atlas__t.html" title="A texture atlas is used to pack several small regions into a single texture.">texture_atlas_t</a> * atlas = <a name="a1"></a><a class="code" href="group__texture-atlas.html#ga7769be70f630e0a8a8b320525a07382d" title="Creates a new empty texture atlas.">texture_atlas_new</a>( 512, 512, 1 );
    <span class="keyword">const</span> <span class="keywordtype">char</span> *filename = <span class="stringliteral">&quot;fonts/Vera.ttf&quot;</span>;
    <span class="keyword">const</span> <span class="keywordtype">wchar_t</span> *cache = L<span class="stringliteral">&quot; !\&quot;#$%&amp;&#39;()*+,-./0123456789:;&lt;=&gt;?&quot;</span>
                           L<span class="stringliteral">&quot;@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_&quot;</span>
                           L<span class="stringliteral">&quot;`abcdefghijklmnopqrstuvwxyz{|}~&quot;</span>;
    <span class="keywordtype">size_t</span> minsize = 8, maxsize = 27;
    <span class="keywordtype">size_t</span> count = maxsize - minsize;
    <span class="keywordtype">size_t</span> i, missed = 0;

    <span class="keywordflow">for</span>( i=minsize; i &lt; maxsize; ++i )
    {
        <a name="_a2"></a><a class="code" href="structtexture__font__t.html" title="Texture font structure.">texture_font_t</a> * font = <a name="a3"></a><a class="code" href="group__texture-font.html#ga904424e7b0e2be14b694599b5c483e64" title="This function creates a new texture font from given filename and size.">texture_font_new</a>( atlas, filename, i );
        missed += <a name="a4"></a><a class="code" href="group__texture-font.html#ga912b65dd3d45ced38640cdfbecbb51c5" title="Request the loading of several glyphs at once.">texture_font_load_glyphs</a>( font, cache );
        <a name="a5"></a><a class="code" href="group__texture-font.html#gaa894ab1def90197eaad58b578e7a2440" title="Delete a texture font.">texture_font_delete</a>( font );
    }

    printf( <span class="stringliteral">&quot;Matched font               : %s\n&quot;</span>, filename );
    printf( <span class="stringliteral">&quot;Number of fonts            : %ld\n&quot;</span>, count );
    printf( <span class="stringliteral">&quot;Number of glyphs per font  : %ld\n&quot;</span>, wcslen(cache) );
    printf( <span class="stringliteral">&quot;Number of missed glyphs    : %ld\n&quot;</span>, missed );
    printf( <span class="stringliteral">&quot;Total number of glyphs     : %ld/%ld\n&quot;</span>,
            wcslen(cache)*count - missed, wcslen(cache)*count );
    printf( <span class="stringliteral">&quot;Texture size               : %ldx%ld\n&quot;</span>, atlas-&gt;<a name="a6"></a><a class="code" href="structtexture__atlas__t.html#aa1fb802d48b70dc43e06e88f55f986aa" title="Width (in pixels) of the underlying texture.">width</a>, atlas-&gt;<a name="a7"></a><a class="code" href="structtexture__atlas__t.html#a724b0e35c619f88b231eba83c39095cb" title="Height (in pixels) of the underlying texture.">height</a> );
    printf( <span class="stringliteral">&quot;Texture occupancy          : %.2f%%\n&quot;</span>, 
            100.0*atlas-&gt;<a name="a8"></a><a class="code" href="structtexture__atlas__t.html#af56de920973c1cb4621e31af7aeaf276" title="Allocated surface size.">used</a>/(<span class="keywordtype">float</span>)(atlas-&gt;<a class="code" href="structtexture__atlas__t.html#aa1fb802d48b70dc43e06e88f55f986aa" title="Width (in pixels) of the underlying texture.">width</a>*atlas-&gt;<a class="code" href="structtexture__atlas__t.html#a724b0e35c619f88b231eba83c39095cb" title="Height (in pixels) of the underlying texture.">height</a>) );

    glClearColor(1,1,1,1);
    glEnable( GL_BLEND );
    glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
    glEnable( GL_TEXTURE_2D );
    glColor4f(0,0,0,1);
    glBindTexture( GL_TEXTURE_2D, atlas-&gt;<a name="a9"></a><a class="code" href="structtexture__atlas__t.html#a03e72bd01b6e5e97c0cc032d22c11c45" title="Texture identity (OpenGL)">id</a> );
    glutMainLoop( );

    <span class="keywordflow">return</span> 0;
}
</pre></div> </div><!-- contents -->
</div>
</body>
</html>
